import Action from './Action'

export default class ImSystem extends Action {
    columns: any
    tableIndexArr: Array<{ name: string; key: string }>

    constructor() {
        super()
        this.tableName = 'im_system'

        this.columns = {
            id: 'BIGINT(20) PRIMARY KEY NOT NULL', //消息id（服务器）
            no: "BIGINT(20) NOT NULL DEFAULT '0'",
            title: 'VARCHAR(64)',
            content: 'VARCHAR(512)',
            imgId: "BIGINT(20) NOT NULL DEFAULT '0'",
            href: 'VARCHAR(128)',
            page: 'VARCHAR(128)',
            isRead: "INT(2) DEFAULT '0'", //阅读状态(0-未读，1-已读)
            createTime: 'datetime DEFAULT NULL',
            updateTime: 'datetime DEFAULT NULL',
            delFlag: "tinyint(1) NOT NULL DEFAULT '0'"
        }

        this.tableIndexArr = [{ name: 'idx_im_system_no', key: 'no' }]
    }

    saveOrUpdateSystem(data: any) {
        return super.saveOrUpdate(data)
    }

    historyList(userId: string, firstTime: any) {
        return new Promise((resolve, reject) => {
            let timeCdnStr = '',
                limit = ''
            if (firstTime) {
                timeCdnStr = `update_time < '${firstTime}'`
                limit = 'limit 10'
            }
            super
                .getList({
                    where: `where ${timeCdnStr} order by update_time desc ${limit}`
                })
                .then((brecords: any) => {
                    if (brecords && brecords.length > 0) {
                        resolve(brecords)
                    } else {
                        resolve([])
                    }
                })
        })
    }

    changeIsReadAll() {
        return super.update({
            whereObj: { isRead: 0 },
            data: {
                isRead: 1
            }
        })
    }
}
